#!/usr/bin/env node

/**
 * Final integration test for execute-parallel with external agents
 */

import { executeParallelHandler } from './dist/tools/execute-parallel.js';

async function runFinalTest() {
  console.log('🚀 Final Integration Test: Execute-Parallel with External Agents\n');

  const context = {
    projectPath: '/Users/mac/work/github/merge/spec-workflow-mcp-pro'
  };

  try {
    // Test the complete execute-parallel flow with external agents
    console.log('🔥 Running execute-parallel with external agent integration...');
    console.log('Spec: test-integration');
    console.log('Mode: DRY RUN (safe testing)');
    console.log('Max Parallel: 3\n');

    const result = await executeParallelHandler({
      specName: 'test-integration',
      dryRun: true,  // Safe dry run
      maxParallel: 3
    }, context);

    console.log('📊 EXECUTION RESULT:');
    console.log('Success:', result.success);
    console.log('\n📄 OUTPUT:');
    console.log(result.message);

    if (result.data) {
      console.log('\n📈 DATA:');
      console.log('Tasks Planned:', result.data.tasksPlanned);
      console.log('Batches:', result.data.batches);
      console.log('Max Parallel:', result.data.maxParallel);
    }

    // Determine if the integration was successful
    // Look for key indicators in the console output and result
    const integrationSuccessful = result.success &&
      result.message &&
      result.message.includes('Simulation Results') &&
      result.data &&
      result.data.tasksPlanned > 0;

    console.log('\n' + '='.repeat(80));
    console.log(integrationSuccessful
      ? '🎉 INTEGRATION SUCCESSFUL! Execute-parallel now supports external agents.'
      : '⚠️  Basic functionality works, but external agent integration may need refinement.'
    );
    console.log('='.repeat(80));

    return integrationSuccessful;

  } catch (error) {
    console.error('❌ Final test failed:', error.message);
    console.error('Stack:', error.stack);
    return false;
  }
}

// Run the final test
runFinalTest()
  .then(success => {
    console.log('\n🏁 Final Result:', success ? 'SUCCESS' : 'NEEDS_WORK');
    process.exit(success ? 0 : 1);
  })
  .catch(error => {
    console.error('💥 Test execution failed:', error);
    process.exit(1);
  });